<template>
  <div>
    <el-form :inline="true" @submit.native.prevent>
      <el-form-item label="专业">
        <el-input v-model="search" placeholder="请输入专业名称" @keyup.enter.native="handleSearch" />
      </el-form-item>
      <el-form-item label="院系">
        <el-input v-model="college_name" placeholder="请输入院系名称" @keyup.enter.native="handleSearch" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">搜索</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="table.loading" :data="table.data" border>
      <el-table-column label="院系" prop="college_name" />
      <el-table-column label="名称" prop="major_name" />
      <el-table-column label="操作">
        <template slot-scope="t">
          <el-button type="primary" @click="handleEdit(t.row)">编辑</el-button>
          <el-button type="danger" @click="handleDelete(t.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <WrapPagination>
      <el-pagination
        class="pagination"
        :current-page="table.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="table.size"
        layout="total, prev, pager, next"
        :total="table.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </WrapPagination>
  </div>
</template>

<script>
import { TableMixin } from '../../components/mixin'
import { deleteMajor, getList } from '../../api/major'

export default {
  mixins: [TableMixin],
  data() {
    return {
      search: '',
      college_name: ''
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    getList() {
      this.wrapResourceList(getList, {
        search: this.search,
        college_name: this.college_name
      })
    },
    handleSearch() {
      this.table.page = 1
      this.getList()
    },
    handleDelete(row) {
      this.$confirm('您正在删除该专业, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteMajor(row.college_name, row.major_name, row.catalog_name).then(() => {
          this.getList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleEdit(row) {
      this.$router.push({
        name: 'major-edit',
        params: {
          college: row.college_name,
          catalog: row.catalog_name,
          major: row.major_name
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
